home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / mac / Sample Code / QuickTime / QuickTimeIntro / Graphic Import⁄Export / MacShell / NavFile.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-06  |  11.6 KB  |  339 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NavFile.c
  3.     
  4.     Description: Get and Put services.
  5.  
  6.     Author:        based on code from QTShell sample
  7.  
  8.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first): <1> 4/1/00 initial release
  46.  
  47. */
  48.  
  49. #include "MacShell.h"
  50.  
  51. const OSType    kApplicationSignature  = FOUR_CHAR_CODE('imex');
  52. const ResType   kOpenResourceType = FOUR_CHAR_CODE('open');
  53. const StringPtr kApplicationName = "\pGraphics Importers & Exporters";
  54.  
  55. //////////
  56. //
  57. // HandleNavEvent
  58. // A callback procedure that handles events while a Navigation Service dialog box is displayed.
  59. //
  60. //////////
  61. static PASCAL_RTN void HandleNavEvent(NavEventCallbackMessage theCallBackSelector, NavCBRecPtr theCallBackParms, void *theCallBackUD);
  62. static PASCAL_RTN void HandleNavEvent(NavEventCallbackMessage theCallBackSelector, NavCBRecPtr theCallBackParms, void *theCallBackUD)
  63. {
  64. #pragma unused(theCallBackUD)
  65.     WindowReference        myWindow = NULL;    
  66.     
  67.     if (theCallBackSelector == kNavCBEvent) {
  68.         switch (theCallBackParms->eventData.eventDataParms.event->what) {
  69.             case updateEvt:
  70. #if TARGET_OS_MAC
  71.                 // Handle Update Event
  72. #endif
  73.                 break;
  74.             case nullEvent:
  75.                 // Handle Null Event
  76.                 break;
  77.         }
  78.     }
  79. }
  80.  
  81. //////////
  82. //
  83. // CreateOpenHandle
  84. // Get the 'open' resource or dynamically create a NavTypeListHandle.
  85. //
  86. //////////
  87. static Handle CreateOpenHandle (OSType theApplicationSignature, short theNumTypes, TypeListPtr theTypeList);
  88. static Handle CreateOpenHandle(OSType theApplicationSignature, short theNumTypes, TypeListPtr theTypeList)
  89. {
  90.     Handle            myHandle = NULL;
  91.     
  92.     // see if we have an 'open' resource...
  93.     myHandle = Get1Resource('open', 128);
  94.     if ( myHandle != NULL && ResError() == noErr ) {
  95.         DetachResource( myHandle );
  96.         return myHandle;
  97.     } else {
  98.         myHandle = NULL;
  99.     }
  100.     
  101.     // nope, use the passed in types and dynamically create the NavTypeList
  102.     if (theTypeList == NULL)
  103.         return myHandle;
  104.     
  105.     if (theNumTypes > 0) {
  106.         myHandle = NewHandle(sizeof(NavTypeList) + (theNumTypes * sizeof(OSType)));
  107.         if (myHandle != NULL) {
  108.             NavTypeListHandle     myOpenResHandle    = (NavTypeListHandle)myHandle;
  109.             
  110.             (*myOpenResHandle)->componentSignature = theApplicationSignature;
  111.             (*myOpenResHandle)->osTypeCount = theNumTypes;
  112.             BlockMoveData(theTypeList, (*myOpenResHandle)->osType, theNumTypes * sizeof(OSType));
  113.         }
  114.     }
  115.     
  116.     return myHandle;
  117. }
  118.  
  119. //////////
  120. //
  121. // GetOneFileWithPreview
  122. // Display the appropriate file-opening dialog box, with an optional QuickTime preview pane. If the user
  123. // selects a file, return information about it using the theFSSpecPtr parameter.
  124. //
  125. // Note that both StandardGetFilePreview and NavGetFile use the function specified by theFilterProc as a
  126. // file filter. This framework always passes NULL in the theFilterProc parameter. If you use this function
  127. // in your own code, keep in mind that on Windows the function specifier must be of type FileFilterUPP and 
  128. // on Macintosh it must be of type NavObjectFilterUPP. (You can use the QTFrame_GetFileFilterUPP to create
  129. // a function specifier of the appropriate type.) Also keep in mind that Navigation Services expects a file 
  130. // filter function to return true if a file is to be displayed, while the Standard File Package expects the
  131. // filter to return false if a file is to be displayed.
  132. //
  133. //////////
  134. OSErr GetOneFileWithPreview (short theNumTypes, TypeListPtr theTypeList, FSSpecPtr theFSSpecPtr, void *theFilterProc)
  135. {
  136.     
  137. #if TARGET_OS_WIN32
  138.     StandardFileReply    myReply;
  139. #endif
  140. #if TARGET_OS_MAC
  141.     NavReplyRecord        myReply;
  142.     NavDialogOptions    myDialogOptions;
  143.     NavTypeListHandle    myOpenList = NULL;
  144.     NavEventUPP            myEventUPP = NewNavEventUPP(HandleNavEvent);
  145. #endif
  146.     OSErr                myErr = noErr;
  147.     
  148.     if (theFSSpecPtr == NULL)
  149.         return(paramErr);
  150.  
  151. #if TARGET_OS_WIN32
  152.     // prompt the user for a file
  153.     StandardGetFilePreview((FileFilterUPP)theFilterProc, theNumTypes, (ConstSFTypeListPtr)theTypeList, &myReply);
  154.     if (!myReply.sfGood)
  155.         return(userCanceledErr);
  156.     
  157.     // make an FSSpec record
  158.     myErr = FSMakeFSSpec(myReply.sfFile.vRefNum, myReply.sfFile.parID, myReply.sfFile.name, theFSSpecPtr);
  159. #endif
  160.  
  161. #if TARGET_OS_MAC
  162.     // specify the options for the dialog box
  163.     NavGetDefaultDialogOptions(&myDialogOptions);
  164.     myDialogOptions.dialogOptionFlags -= kNavNoTypePopup;
  165.     myDialogOptions.dialogOptionFlags -= kNavAllowMultipleFiles;
  166.     BlockMoveData(kApplicationName, myDialogOptions.clientName, kApplicationName[0] + 1);
  167.     
  168.     // create a handle to an 'open' resource
  169.     myOpenList = (NavTypeListHandle)CreateOpenHandle(kApplicationSignature, theNumTypes, theTypeList);
  170.     if (myOpenList != NULL)
  171.         HLock((Handle)myOpenList);
  172.     
  173.     // prompt the user for a file
  174.     myErr = NavGetFile(NULL, &myReply, &myDialogOptions, myEventUPP, NULL, (NavObjectFilterUPP)theFilterProc, myOpenList, NULL);
  175.     if ((myErr == noErr) && myReply.validRecord) {
  176.         AEKeyword        myKeyword;
  177.         DescType        myActualType;
  178.         Size            myActualSize = 0;
  179.         
  180.         // get the FSSpec for the selected file
  181.         if (theFSSpecPtr != NULL)
  182.             myErr = AEGetNthPtr(&(myReply.selection), 1, typeFSS, &myKeyword, &myActualType, theFSSpecPtr, sizeof(FSSpec), &myActualSize);
  183.  
  184.         NavDisposeReply(&myReply);
  185.     }
  186.     
  187.     if (myOpenList != NULL) {
  188.         HUnlock((Handle)myOpenList);
  189.         DisposeHandle((Handle)myOpenList);
  190.     }
  191.     
  192.     DisposeNavEventUPP(myEventUPP);
  193. #endif
  194.  
  195.     return(myErr);
  196. }
  197.  
  198. //////////
  199. //
  200. // PutFile
  201. // Save a file under the specified name. Return Boolean values indicating whether the user selected a file
  202. // and whether the selected file is replacing an existing file.
  203. //
  204. //////////
  205.  
  206. OSErr PutFile(ConstStr255Param thePrompt, ConstStr255Param theFileName, FSSpecPtr theFSSpecPtr, Boolean *theIsSelected, Boolean *theIsReplacing)
  207. {
  208. #if TARGET_OS_WIN32
  209.     StandardFileReply    myReply;
  210. #endif
  211. #if TARGET_OS_MAC
  212.     NavReplyRecord        myReply;
  213.     NavDialogOptions    myDialogOptions;
  214.     NavEventUPP            myEventUPP = NULL; // NewNavEventUPP(HandleNavEvent);
  215. #endif
  216.     OSErr                myErr = noErr;
  217.  
  218.     if ((theFSSpecPtr == NULL) || (theIsSelected == NULL) || (theIsReplacing == NULL))
  219.         return(paramErr);
  220.  
  221.     // assume we are not replacing an existing file
  222.     *theIsReplacing = false;
  223.     
  224. #if TARGET_OS_WIN32
  225.     StandardPutFile(thePrompt, theFileName, &myReply);
  226.     *theFSSpecPtr = myReply.sfFile;
  227.     *theIsSelected = myReply.sfGood;
  228.     if (myReply.sfGood)
  229.         *theIsReplacing = myReply.sfReplacing;
  230. #endif
  231.  
  232. #if TARGET_OS_MAC
  233.     // specify the options for the dialog box
  234.     NavGetDefaultDialogOptions(&myDialogOptions);
  235.     myDialogOptions.dialogOptionFlags += kNavNoTypePopup;
  236.     myDialogOptions.dialogOptionFlags += kNavDontAutoTranslate;
  237.     BlockMoveData(theFileName, myDialogOptions.savedFileName, theFileName[0] + 1);
  238.     BlockMoveData(kApplicationName, myDialogOptions.clientName, kApplicationName[0] + 1);
  239.     BlockMoveData(thePrompt, myDialogOptions.message, thePrompt[0] + 1);
  240.     
  241.     // prompt the user for a file
  242.     myErr = NavPutFile(NULL, &myReply, &myDialogOptions, myEventUPP, MovieFileType, sigMoviePlayer, NULL);
  243.     if ((myErr == noErr) && myReply.validRecord) {
  244.         AEKeyword        myKeyword;
  245.         DescType        myActualType;
  246.         Size            myActualSize = 0;
  247.         
  248.         // get the FSSpec for the selected file
  249.         if (theFSSpecPtr != NULL)
  250.             myErr = AEGetNthPtr(&(myReply.selection), 1, typeFSS, &myKeyword, &myActualType, theFSSpecPtr, sizeof(FSSpec), &myActualSize);
  251.  
  252.         NavDisposeReply(&myReply);
  253.     }
  254.         
  255.     *theIsSelected = myReply.validRecord;
  256.     if (myReply.validRecord)
  257.         *theIsReplacing = myReply.replacing;
  258.  
  259.     DisposeNavEventUPP(myEventUPP);
  260. #endif
  261.  
  262.     return(myErr);
  263. }
  264.  
  265. static OSErr AddFileType( OSType fileType, Handle list, long *count );
  266. static OSErr AddFileType( OSType fileType, Handle list, long *count )
  267. {
  268.     OSErr err = noErr;
  269.  
  270.     err = PtrAndHand( &fileType, list, sizeof(OSType) );
  271.     if ( noErr == err )
  272.         (*count) += 1;
  273.     
  274.     return err;
  275. }
  276.  
  277. static OSErr AddComponentFileTypes( OSType componentType, Boolean suffixInstead, Handle list, long *count );
  278. static OSErr AddComponentFileTypes( OSType componentType, Boolean suffixInstead, Handle list, long *count )
  279. {
  280.     OSErr err = noErr;
  281.     
  282.     ComponentDescription cd = {0,0,0,0,0};
  283.     Component c;
  284.  
  285.     cd.componentType = componentType;
  286.     cd.componentFlags = suffixInstead ? movieImportSubTypeIsFileExtension : 0;
  287.     cd.componentFlagsMask = movieImportSubTypeIsFileExtension;
  288.  
  289.     c = 0;
  290.     while( 0 != ( c = FindNextComponent( c, &cd ))) {
  291.         ComponentDescription d;
  292.         GetComponentInfo( c, &d, NULL, NULL, NULL );
  293.         err = AddFileType( d.componentSubType, list, count );
  294.         if ( err ) break;
  295.     }
  296.     
  297.     return err;
  298. }
  299.  
  300. OSErr BuildGraphicsImporterValidFileTypes( Handle list, long *count )
  301. {
  302.     return AddComponentFileTypes( GraphicsImporterComponentType, false, list, count );
  303. }
  304.  
  305. OSErr BuildGraphicsImporterValidFileNameSuffixes( Handle list, long *count )
  306. {
  307.     return AddComponentFileTypes( GraphicsImporterComponentType, true, list, count );
  308. }
  309.  
  310. OSErr BuildMovieValidFileTypes( Handle list, long *count )
  311. {
  312.     return AddFileType( kQTFileTypeMovie, list, count );
  313. }
  314.  
  315. OSErr BuildAllValidFileTypes( Handle list, long *count )
  316. {
  317.     OSErr err = noErr;
  318.     
  319.     err = AddFileType( kQTFileTypeMovie, list, count );
  320.     if ( noErr == err )
  321.         err = AddComponentFileTypes( MovieImportType, false, list, count );
  322.         if ( noErr == err )
  323.             err = AddComponentFileTypes( GraphicsImporterComponentType, false, list, count );
  324.             
  325.     return err;
  326. }
  327.  
  328. OSErr BuildAllValidFileNameSuffixes( Handle list, long *count )
  329. {
  330.     OSErr err = noErr;
  331.     
  332.     err = AddFileType( FOUR_CHAR_CODE('MOV '), list, count );
  333.     if ( noErr == err )
  334.         err = AddComponentFileTypes( MovieImportType, true, list, count );
  335.         if ( noErr == err )
  336.             err = AddComponentFileTypes( GraphicsImporterComponentType, true, list, count );
  337.             
  338.     return err;
  339. }